package com.hkzhao.reportingsystem.controller;

import com.hkzhao.reportingsystem.common.api.CommonPage;
import com.hkzhao.reportingsystem.common.api.CommonResult;
import com.hkzhao.reportingsystem.dto.AdminLoginParam;
import com.hkzhao.reportingsystem.dto.CaseDto;
import com.hkzhao.reportingsystem.mbg.model.Case;
import com.hkzhao.reportingsystem.service.DoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//主要用于医生一些常规操作的controller
@Api(tags = "DoctorController",description = "医护人员操作管理")
@Controller
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @ApiOperation("医护人员的注册申请")
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult register(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String Worder_id,
                                 @RequestParam String Identify_id,
                                 @RequestParam String Doctor_type,
                                 @RequestParam String Province,
                                 @RequestParam String email,
                                 @RequestParam String ValidCode){
        return   doctorService.register(username,password,Worder_id,Doctor_type,
                Province,Identify_id,ValidCode,email);
    }
    @ApiOperation("获取验证码")
    @RequestMapping(value = "/getvalidcode",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getvalidCode(@RequestParam String email){
        return doctorService.sendDelayReigsterEmail(email);
    }

    @ApiOperation("登录以后返回token")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody AdminLoginParam adminLoginParam, BindingResult bindingResult)
    {
        String token = doctorService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword());
        if(token==null)
            return CommonResult.validateFailed("用户名或者密码错误");
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return CommonResult.success(tokenMap);
    }
    @ApiOperation("病例插入")
    @RequestMapping(value = "/addCase",method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('doctor:write_and_read')")
    public CommonResult addCase(@RequestParam(value = "locate") @ApiParam("定位") String Locate,
                                @RequestParam(value = "note") @ApiParam("备注")String Note,
                                @RequestParam(value = "telephone") @ApiParam("电话号码") String Telephone,
                                @RequestParam(value = "age") @ApiParam("年龄") String Age,
                                @RequestParam(value = "level") @ApiParam("危重等级")String Level,
                                @RequestParam(value = "disease_type") @ApiParam("疾病类型")String Disease_typem,
                                @RequestParam(value = "gender") @ApiParam("年龄")String gender,
                                HttpServletRequest request)
    {
        return doctorService.addCase(Locate,Note,Telephone,Integer.parseInt(Age),Level,Disease_typem,gender,request);
    }
    @ApiOperation("查询自己操作的病例")
    @RequestMapping(value = "/getCaseList",method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('doctor:write_and_read')")
    public CommonResult<CommonPage<Case>> getCaseList (HttpServletRequest request,
                                                       @RequestParam @ApiParam("页码")int pageNum,
                                                       @RequestParam @ApiParam("数量")int pageSize)
    {
        List<Case> list = doctorService.getCaseByusername(request,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("插入审核病例")
    @RequestMapping(value = "/addAuthCase",method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('doctor:write_and_read')")
    public CommonResult addAuthCase(@RequestParam CaseDto c)
    {   c.setTime(new Date());
       return doctorService.addAuthCase(c);
    }
}
